(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('accountService', accountService);
	accountService.$inject = [ '$q', 'loggerFactory', 'ajaxServiceFactory', 'modalService'];
	function accountService($q, loggerFactory, ajaxServiceFactory, modalService) {
		var accountServiceObj = this;
		var log = loggerFactory.getLogger("accountService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getAccountData :{
				url : 'do/cda/dataList/accountDatas'
			},
			getPatternNames : {
				url : 'do/cda/dataList/acctPattern',
				method : 'get'
			},
			getSearchAccountData :{
				url : 'do/cda/dataList/searchAccountDatas'
			}
		});
		
		function initOptions(){
			var defer = $q.defer();
			ajaxService.getAccountData().then(function(response) {
				var object = {};
				// get acctPatternFlagOptions and splitFlagOptions
				object.custAcctFlagOptions = response.data.acctPatternFlagOptions;
				var splitFlagOptions = {};
				splitFlagOptions.data = [];
				object.acctPatternFlagOptions = [];
				if(response && response.data.acctPatternFlagOptions){
					angular.forEach(response.data.acctPatternFlagOptions, function(data, index, array){
						object.acctPatternFlagOptions.push({"value" : data.value, "label" : data.label});
						splitFlagOptions.data.push({"value" : data.value, "label" : data.label});
					});
			    }
				object.splitFlagOptions = splitFlagOptions;
	
			    // get summaryFlag1Options and  summaryFlag2Options
				var summaryFlag1Options = {};
				var summaryFlag2Options = {};
				summaryFlag1Options.data = response.data.summaryFlagOptions;
				summaryFlag2Options.data = angular.copy(response.data.summaryFlagOptions);
				object.summaryFlag1Options = summaryFlag1Options;
				object.summaryFlag2Options = summaryFlag2Options;
				
				// get acctTypeOptions
                var acctTypeOptions = {};
                acctTypeOptions.data = response.data.acctTypeOptions ? response.data.acctTypeOptions : [];
                object.acctTypeOptions = acctTypeOptions;
					
				// get partyTypeOptions
				var partyTypeOptions = {};
				partyTypeOptions.data = response.data.partyTypeOptions ? response.data.partyTypeOptions : [];
				object.partyTypeOptions = partyTypeOptions;
				
				// get acctCcyOptions
				var acctCcyOptions = {};
				acctCcyOptions.data = response.data.acctCcyOptions ? response.data.acctCcyOptions : [];
				object.acctCcyOptions = acctCcyOptions;
				
				// get acctNoOptions
				var acctNoOptions ={};
				acctNoOptions.data = response.data.acctNoOptions ? response.data.acctNoOptions : [];
				object.acctNoOptions = acctNoOptions;
				
				// get accountPatterns
				object.accountPatterns = [];
				object.accountPatternsTwo = [];
				if(response.data.accountPatterns){
					var s = [];
					angular.forEach(response.data.accountPatterns, function(data,
							index, array) {
						if (s.indexOf(data.value) === -1) {
							s.push(data.value);
						}
					});
					if (s.length > 0) {
						for (var i = 0; i < s.length; i++) {
							object.accountPatterns.push({
								"value" : '{'+s[i]+'}',
								"label" : s[i]
							});
						};
						for (var j = 0; j < s.length; j++) {
							object.accountPatternsTwo.push({
								"value" : '{'+s[j]+'}',
								"label" : s[j]
							});
						};
					};
				}
				defer.resolve(object);
			}, angular.noop);
			return defer.promise;
		};
		
		function getPatternName(){
			var defer = $q.defer();
			var patternNames = [];
			ajaxService.getPatternNames().then(function(response) {
				if(response.data){
					var s = [];
					angular.forEach(response.data, function(data,
							index, array) {
						if (s.indexOf(data.value) === -1) {
							s.push(data.value);
						}
					});
					if (s.length > 0) {
						for (var i = 0; i < s.length; i++) {
							patternNames.push({
								"value" : '{'+s[i]+'}',
								"label" : s[i]
							});
						}
					}
				}
				defer.resolve(patternNames);
			}, angular.noop);
			return defer.promise;
		};
		
		
		function initOptionForSearch(){
			var defer = $q.defer();
			ajaxService.getSearchAccountData().then(function(response) {
				defer.resolve(response ? response.data : {});
			}, angular.noop);
			return defer.promise;
		};
		
		return {
			initOptions : initOptions,
			initOptionForSearch : initOptionForSearch,
			getPatternName : getPatternName
		};
	}
})(angular);
